package goodroommate.ui.questionnaire;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import goodroommate.domain.Questionnaire;
import goodroommate.exception.DatabaseException;
import goodroommate.service.IQuestionnaireService;
import goodroommate.service.impl.QuestionnaireService;

/**
 * Servlet implementation class AddQuestionnaireServlet
 */
@WebServlet("/Questionnaire/AddQuestionnaire")
public class AddQuestionnaireServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	IQuestionnaireService questionnaireService = new QuestionnaireService();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddQuestionnaireServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("addquestionnaire.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String subject = request.getParameter("subject");
		String strstarttime = request.getParameter("starttime");
		String strendtime = request.getParameter("endtime");
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm" + ":00");


		Timestamp startstamp = java.sql.Timestamp.valueOf(strstarttime + ":00");
		Timestamp endstamp = java.sql.Timestamp.valueOf(strendtime + ":00");

		try{
			Questionnaire questionnaire=questionnaireService.addQuestionnaire(subject, startstamp, endstamp);

			response.sendRedirect("AddQuestion?questionnaireid=" + questionnaire.getId());
			return;
		} catch (DatabaseException e) {
			response.sendError(500);
			return;
		}
		

	}

}
